package com.dev.dream.exochatlockscreen;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.dev.dream.exochatlockscreen.service.SLockscreen;
import com.dev.dream.libs.adapter.ImageAdapterGridView;
import com.dev.dream.libs.adapter.ImageBitmap;
import com.dev.dream.libs.listviewanimation.simple.SwingBottomInAnimationAdapter;
import com.startapp.android.publish.StartAppSDK;

@SuppressLint("NewApi")
public class ChangeAvatarGridview extends Activity {

	private static final int INITIAL_DELAY_MILLIS = 300;

	GridView gridView;
	ImageAdapterGridView customAdapterGridView;
	ArrayList<ImageBitmap> icons;
	com.startapp.android.publish.banner.Banner banner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.change_background_gridview);

		StartAppSDK.init(this, "111076217", "210808285", true);

		gridView = (GridView) findViewById(R.id.gird_icon);
		icons = new ArrayList<ImageBitmap>();
		
		if (SLockscreen.instance == null) {

			startService(new Intent(getApplicationContext(),
					SLockscreen.class));

		}

		if (!SLockscreen.instance.loadAvatar) {
			SLockscreen.instance.loadAvatar = true;
			SLockscreen.instance.loadAvatarIcon();
		}
		for (int i = 1; i <= EXOChatKeypadPreferences.getInstance().size_icon; i++) {
			icons.add(new ImageBitmap(i, SLockscreen.instance.avatarIcon[i],
					false));
		}

		customAdapterGridView = new ImageAdapterGridView(
				getApplicationContext(), icons);

		SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
				customAdapterGridView);
		swingBottomInAnimationAdapter.setAbsListView(gridView);

		assert swingBottomInAnimationAdapter.getViewAnimator() != null;
		swingBottomInAnimationAdapter.getViewAnimator().setInitialDelayMillis(
				INITIAL_DELAY_MILLIS);

		gridView.setAdapter(swingBottomInAnimationAdapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub

				// Bundle b = new Bundle();
				// b.putInt("SELECTED_AVATAR", arg2 + 1);
				// i.putExtra("BACK_AVATAR_DATA", b);
				EXOChatKeypadPreferences.getInstance().initPreferences(
						getApplicationContext());
				EXOChatKeypadPreferences.getInstance().setIconExo(arg2 + 1);
				// Intent i = new Intent(getApplicationContext(),
				// Preferences.class);
				// startActivity(i);
				finish();
			}
		});

	}

	public Bitmap getBitmapFromAssets(String fileName) {

		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream(getAssets().open(fileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitmap;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();

	}

	public void loadDataFromAsset() {

		try {
			// get input stream
			InputStream ims = getAssets().open("avatar.jpg");
			// load image as Drawable
			Drawable d = Drawable.createFromStream(ims, null);
			// set image to ImageView

		} catch (IOException ex) {
			return;
		}

	}
}
